﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using AdvanceLib.ScriptCore;
using AdvanceLib.Core;
#endregion

namespace AdvanceLib.Extension.XseScript
{
    public class ScriptParser : IParser
    {
        #region Constructors
        public ScriptParser(XseExtensionProvider provider)
        {
            this._provider = provider;
            this._database = provider.Database;
        }
        #endregion

        #region Fields
        private XseExtensionProvider _provider;
        private ScriptDatabase _database;
        #endregion

        #region IParser Member
        public List<ICommand> Parse(string input)
        {
            List<ICommand> output = new List<ICommand>();

            LineParser lineParser = new LineParser();
            lineParser.Parse(input);

            foreach (Line line in lineParser.Lines)
            {
                if (!string.IsNullOrEmpty(line.Content))
                {
                    ScriptCommand command = new ScriptCommand(this._provider);
                    CommandData commandData = this._database.GetCommandDataByName(line.Parts[0]);

                    command.Name = line.Parts[0];
                    command.HexCode = commandData.HexCode;

                    for (int i = 1; i < line.Parts.Length; i++)
                    {
                        if (line.Parts[i][Constants.FIRST_INDEX] != '@')
                        {
                            byte[] data = BitConverter.GetBytes(Convert.ToInt32(line.Parts[i]));

                            command.Parameters.Add(new ScriptParam()
                            {
                                Value = data,
                                Length = commandData.Parameters[i - 1],
                                DynamicName = string.Empty
                            });
                        }
                        else
                        {
                            command.Parameters.Add(new ScriptParam()
                            {
                                Length = 4,
                                DynamicName = line.Parts[i]
                            });
                        }
                    }

                    output.Add(command);
                }
            }

            return output;
        }
        public string Deparse(List<ICommand> input)
        {
            StringBuilder output = new StringBuilder();

            foreach (ScriptCommand command in input)
            {
                CommandData commandData = this._database.GetCommandDataByHexCode(command.HexCode);

                output.Append(commandData.Name);
                foreach (ScriptParam param in command.Parameters)
                {
                    output.Append(Constants.WHITE_SPACE);
                    if (param.IsDynamic)
                    {
                        output.Append(param.DynamicName);
                    }
                    else
                    {
                        int value = BitConverter.ToInt32(param.Value, 0);

                        output.Append("0x");
                        output.Append(value.ToString("X2"));
                    }
                }
                output.Append(Constants.NEW_LINE);
            }

            return output.ToString();
        }
        #endregion
    }
}
